import { createRouter, createWebHistory } from 'vue-router'

//导入组件
import LoginVue from '@/views/Login.vue'
import Main from "@/views/Main.vue";
import Register from "@/views/components/RegisterPage.vue"
import {useTokenStore} from '@/stores/token.js'
//定义路由关系
const routes = [
    { path: '/', redirect: 'login', component: Main},
    { path: '/main', component: Main},
    { path: '/login',name:'login', component: LoginVue },
    { path: '/register', component: Register},
]

//创建路由器
const router = createRouter({
    history: createWebHistory(),
    routes: routes
})
router.beforeEach((to, from, next) => {
    const tokenStore = useTokenStore()
    console.log("22"+tokenStore.token)
    if (to.name !== 'login' && !tokenStore.token) {
        // 强制跳转到登录页面
        next({ name: 'login' })
    } else {
        // 否则允许路由跳转
        next()
    }
})
//导出路由
export default router